﻿using System;
using System.Collections.Generic;
using System.Text;

namespace DotNETX.Utility
{
    public static class NumberUtils
    {
        public static int? AttemptConvertInt(string value)
        {
            int result;

            if (int.TryParse(value, out result))
            {
                return result;
            }

            return null;
        }

        public static decimal? AttemptConvertDecimal(string value)
        {
            decimal result;

            if (decimal.TryParse(value, out result))
            {
                return result;
            }

            return null;
        }

        public static double? AttemptConvertDouble(string value)
        {
            double result;

            if (double.TryParse(value, out result))
            {
                return result;
            }

            return null;
        }

        public static char IntToHex(int n)
        {
            if (n <= 9)
            {
                return (char)(n + 48);
            }
            return (char)((n - 10) + 97);
        }

        public static int HexToInt(char h)
        {
            if ((h >= '0') && (h <= '9'))
            {
                return (h - '0');
            }
            if ((h >= 'a') && (h <= 'f'))
            {
                return ((h - 'a') + 10);
            }
            if ((h >= 'A') && (h <= 'F'))
            {
                return ((h - 'A') + 10);
            }
            return -1;
        }
    }
}
